package com.yxc.random;

import java.util.*;

/**
 * 随机工具类
 *
 * @author: 杨旭晨
 * @time: 2021/11/11 11:28
 **/
public class RandomUtils {

    //中国移动
    public static final String[] CHINA_MOBILE = {
            "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159",
            "182", "183", "184", "187", "188", "178", "147", "172", "198"
    };
    //中国联通
    public static final String[] CHINA_UNICOM = {
            "130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166"
    };
    //中国电信
    public static final String[] CHINA_TELECOME = {
            "133", "149", "153", "173", "177", "180", "181", "189", "199"
    };
    // 随机生成名字相关
    public static final String familyName =
            "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻水云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅卞齐康伍余元卜顾孟平"
                    + "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计成戴宋茅庞熊纪舒屈项祝董粱杜阮席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田胡凌霍万柯卢莫房缪干解应宗丁宣邓郁单杭洪包诸左石崔吉"
                    + "龚程邢滑裴陆荣翁荀羊甄家封芮储靳邴松井富乌焦巴弓牧隗山谷车侯伊宁仇祖武符刘景詹束龙叶幸司韶黎乔苍双闻莘劳逄姬冉宰桂牛寿通边燕冀尚农温庄晏瞿茹习鱼容向古戈终居衡步都耿满弘国文东殴沃曾关红游盖益桓公晋楚闫";
    public static final String firstName2 = "欧阳太史端木上官司马东方独孤南宫万俟闻人夏侯诸葛尉迟公羊赫连澹台皇甫宗政濮阳公冶太叔申屠公孙慕容仲孙钟离长孙宇文司徒鲜于司空闾丘子车亓官司寇巫马公西颛孙壤驷公良漆雕乐正宰父谷梁拓跋夹谷轩辕令狐段干百里呼延东郭南门羊舌微生公户公玉公仪梁丘公仲公上公门公山公坚左丘公伯西门公祖第五公乘贯丘公皙南荣东里东宫仲长子书子桑即墨达奚褚师吴铭";
    public static final String girlName = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽";
    public static final String boyName = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";


    /**
     * 生成随机数字串
     *
     * @param length: 串长度
     * @return java.lang.String
     * @author 杨旭晨
     * @date 2021/10/21 14:58
     **/
    public static String generateNumber(int length) {

        //初始化备选数组
        int[] defaultNums = new int[10];

        for (int i = 0; i < defaultNums.length; i++) {
            defaultNums[i] = i;
        }

        Random random = new Random();

        int[] nums = new int[length];

        //默认数组中可以选择的部分长度
        int canBeUsed = 10;

        //填充目标数组
        for (int i = 0; i < nums.length; i++) {
            //将随机选取的数字存入目标数组
            int index = random.nextInt(canBeUsed);
            nums[i] = defaultNums[index];
            //将已用过的数字扔到备选数组最后，并减小可选区域
//            swap(index, canBeUsed - 1, defaultNums);
//            canBeUsed--;
        }

        StringBuffer str = new StringBuffer();

        for (int i = 0; i < nums.length; i++) {
            str.append(nums[i]);
        }
        return str.toString();
    }

    /**
     * 生成随机mac地址
     *
     * @return java.lang.String
     * @author 杨旭晨
     * @date 2021/10/21 14:58
     **/
    public static String randomMac4Qemu() {
        Random random = new Random();
        String[] mac = {
                String.format("%02x", 0x52),
                String.format("%02x", 0x54),
                String.format("%02x", 0x00),
                String.format("%02x", random.nextInt(0xff)),
                String.format("%02x", random.nextInt(0xff)),
                String.format("%02x", random.nextInt(0xff))
        };
        return String.join("-", mac);
    }

    private static void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    /**
     * @Description list 随机取数据
     * @params list    list集合
     * num     随机取多少条
     **/
    public static List getRandomList(List list, int num) {
        List olist = new ArrayList<>();
        if (list.size() <= num) {
            return list;
        } else {
            Random random = new Random();
            for (int i = 0; i < num; i++) {
                int intRandom = random.nextInt(list.size() - 1);
                olist.add(list.get(intRandom));
                list.remove(list.get(intRandom));
            }
            return olist;
        }
    }

    /*
     * 随机生成国内IP地址
     */
    public static String getRandomIp() {

        //ip范围
        int[][] range = {{607649792, 608174079},//36.56.0.0-36.63.255.255
                {1038614528, 1039007743},//61.232.0.0-61.237.255.255
                {1783627776, 1784676351},//106.80.0.0-106.95.255.255
                {2035023872, 2035154943},//121.76.0.0-121.77.255.255
                {2078801920, 2079064063},//123.232.0.0-123.235.255.255
                {-1950089216, -1948778497},//139.196.0.0-139.215.255.255
                {-1425539072, -1425014785},//171.8.0.0-171.15.255.255
                {-1236271104, -1235419137},//182.80.0.0-182.92.255.255
                {-770113536, -768606209},//210.25.0.0-210.47.255.255
                {-569376768, -564133889}, //222.16.0.0-222.95.255.255
        };

        Random rdint = new Random();
        int index = rdint.nextInt(10);
        String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
        return ip;
    }

    /*
     * 将十进制转换成ip地址
     */
    public static String num2ip(int ip) {
        int[] b = new int[4];
        String x = "";

        b[0] = (int) ((ip >> 24) & 0xff);
        b[1] = (int) ((ip >> 16) & 0xff);
        b[2] = (int) ((ip >> 8) & 0xff);
        b[3] = (int) (ip & 0xff);
        x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);

        return x;
    }

    /**
     * 生成手机号
     *
     * @param op 0 移动 1 联通 2 电信
     */
    public static String createMobile(int op) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        String mobile01;//手机号前三位
        int temp;
        switch (op) {
            case 0:
                mobile01 = CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)];
                break;
            case 1:
                mobile01 = CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)];
                break;
            case 2:
                mobile01 = CHINA_TELECOME[random.nextInt(CHINA_TELECOME.length)];
                break;
            default:
                mobile01 = "op标志位有误！";
                break;
        }
        if (mobile01.length() > 3) {
            return mobile01;
        }
        sb.append(mobile01);
        //生成手机号后8位
        for (int i = 0; i < 8; i++) {
            temp = random.nextInt(10);
            sb.append(temp);
        }
        return sb.toString();
    }

    public static List<String> getPhone(Integer num) {
        Random random = new Random();
        List<String> phoneList = new ArrayList<>();
        for (Integer i = 0; i < num; i++) {
            String mobile = createMobile(random.nextInt(3));
            phoneList.add(mobile);
        }
        return phoneList;
    }

    public static String getPhone() {
        return createMobile(new Random().nextInt(3));
    }

    /**
     * 随机生成imei
     *
     * @return java.lang.String
     * @author YXC
     * @date 2022/10/14 14:36
     **/
    public static String getIMEI() {// calculator IMEI
        int r1 = 1000000 + new Random().nextInt(9000000);
        int r2 = 1000000 + new Random().nextInt(9000000);
        String input = r1 + "" + r2;
        char[] ch = input.toCharArray();
        int a = 0, b = 0;
        for (int i = 0; i < ch.length; i++) {
            int tt = Integer.parseInt(ch[i] + "");
            if (i % 2 == 0) {
                a = a + tt;
            } else {
                int temp = tt * 2;
                b = b + temp / 10 + temp % 10;
            }
        }
        int last = (a + b) % 10;
        if (last == 0) {
            last = 0;
        } else {
            last = 10 - last;
        }
        return input + last;
    }

    /**
     * 随机获取集合当中的一个
     *
     * @param data: 集合
     * @return T
     * @author YXC
     * @date 2022/10/14 14:45
     **/
    public static <T> T randomOne(Collection<T> data) {
        if (data == null || data.isEmpty()) {
            return null;
        }
        Random random = new Random();
        int i = random.nextInt(data.size());
        int flag = 0;
        for (T datum : data) {
            if (flag == i) {
                return datum;
            }
            flag++;
        }
        return null;
    }

    /**
     * 随机生成姓
     *
     * @return java.lang.String
     * @author YXC
     * @date 2022/10/18 16:04
     **/
    public static String randFamilyName() {
        String str = "";
        int strLen;
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (randNum == 1) {
            strLen = familyName.length();
            index = new Random().nextInt(strLen);
            str = String.valueOf(familyName.charAt(index));
        } else {
            strLen = firstName2.length();
            index = new Random().nextInt(strLen);
            if (index % 2 == 0) {
                str = firstName2.substring(index, index + 2);
            } else {
                str = firstName2.substring(index - 1, index + 1);
            }
        }
        return str;
    }

    /**
     * 随机生成性别
     *
     * @return java.lang.String
     * @author YXC
     * @date 2022/10/18 16:05
     **/
    public static String randSex() {
        int randNum = new Random().nextInt(2) + 1;
        return randNum == 1 ? "男" : "女";
    }

    /**
     * 根据性别随机生成名字
     *
     * @param sex: 性别
     * @return java.lang.String
     * @author YXC
     * @date 2022/10/18 16:05
     **/
    public static String randName(String sex) {
        String name = "";
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (sex.equals("男")) {
            int strLen = boyName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = boyName.substring(index, index + randNum);
            } else {
                index = new Random().nextInt(strLen);
                name = boyName.substring(index, index + randNum);
            }
        } else {
            int strLen = girlName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = girlName.substring(index, index + randNum);
            } else {
                index = new Random().nextInt(strLen);
                name = girlName.substring(index, index + randNum);
            }
        }
        return name;
    }

    /**
     * 随机生成年纪
     *
     * @return int
     * @author YXC
     * @date 2022/10/18 16:05
     **/
    public static int randAge() {
        return new Random().nextInt(4) + 18;
    }

    /**
     * 随机生成名字
     *
     * @return java.lang.String
     * @author YXC
     * @date 2022/10/18 16:10
     **/
    public static String randomName() {
        return randFamilyName() + randName(randSex());
    }

    /**
     * 随机生成一个域名地址
     *
     * @return java.lang.String
     * @author YXC
     * @date 2023/11/26 14:58
     **/
    public static String getRandomDomain() {
        String[] topLevelDomains = {"com", "net", "org", "info", "biz", "edu", "gov", "mil"};
        String[] secondLevelDomains = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        String[] thirdLevelDomains = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

        Random random = new Random();
        int tldIndex = random.nextInt(topLevelDomains.length);
        int sldIndex = random.nextInt(secondLevelDomains.length);
        int tldLength = random.nextInt(5) + 2; // 2 to 6 characters for TLD

        String tld = topLevelDomains[tldIndex];
        String sld = secondLevelDomains[sldIndex];
        String tldPart = String.valueOf(random.nextInt(10)); // First character of TLD part is a number

        for (int i = 1; i < tldLength; i++) {
            tldPart += secondLevelDomains[random.nextInt(secondLevelDomains.length)];
        }

        String domainName = sld + "." + tldPart + "." + tld;

        return domainName;
    }

    /**
     * 生成一个随机的RR值
     *
     * @return java.lang.String
     * @author YXC
     * @date 2023/11/26 15:01
     **/
    public static String getRandomRR() {
        String[] domains = new String[100];
        for (int i = 0; i < 100; i++) {
            domains[i] = getRandomDomain();
        }
        String[] types = {"A", "CNAME", "MX"};
        int[] ttls = {3600, 7200, 1800};

        Random random = new Random();
        String domain = domains[random.nextInt(domains.length)];
        String type = types[random.nextInt(types.length)];
        int ttl = ttls[random.nextInt(ttls.length)];

//        System.out.println(domain + " " + type + " " + ttl);
        return domain + " " + type + " " + ttl;
    }

    /**
     * 生成随机字符串
     *
     * @param length: 字符串长度
     * @return java.lang.String
     * @author YXC
     * @date 2024/1/16 14:31
     **/
    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            stringBuilder.append(characters.charAt(index));
        }

        return stringBuilder.toString();
    }

    /**
     * 随机生成从start到end的整数
     *
     * @param start: 开始
     * @param end:   结束
     * @return int
     * @author YXC
     * @date 2024/1/23 10:24
     **/
    public static int randomNum(Integer start, Integer end) {
        Random random = new Random();
        int randomNumber = start + random.nextInt(end - start + 1); // 生成m-n的随机整数
        return randomNumber;
    }

    /**
     * 随机生成从start到end的浮点数
     *
     * @param start: 开始
     * @param end:   结束
     * @return java.lang.Double
     * @author YXC
     * @date 2024/1/23 10:28
     **/
    public static Double randomDouble(Double start, Double end) {
        if (start >= end) {
            throw new IllegalArgumentException("开始值必须小于结束值");
        }
        return Math.random() * (end - start) + start;
    }

    /**
     * 将list当中的元素顺序随机打乱，不改变原数据
     *
     * @param dataList: list数据
     * @return java.util.List<T>
     * @author YXC
     * @date 2024/1/23 11:19
     **/
    public static <T> List<T> randomSequence(List<T> dataList) {
        List<T> shuffledList = new ArrayList<>(dataList);
        Collections.shuffle(shuffledList);
        return shuffledList;
    }
}
